<?php
/**
 * Created by PhpStorm.
 * User: 徐州
 * Date: 2017/5/4
 * Time: 13:39
 */

namespace App\Models\VoteCandidate;

class VoteCandidateRepository implements \App\Contracts\Repositories\VoteCandidate\VoteCandidateRepository
{
    public function getByActivity($activityId, $columns = ['*'])
    {
        return VoteCandidate::query()
            ->where('activity_id', $activityId)
            ->where('status', VoteCandidate::SHELVES_ON)
            // ->orderBy('group_id', 'ASC')
            ->orderBy('weight', 'DESC')
            ->orderBy('code', 'ASC')
            ->get($columns);
    }

    public function getAll()
    {
        return VoteCandidate::with('voteGroup', 'activity')->get();
    }

    public function updateResourceUrl($resourceID, $resourceUrl)
    {
        $voteCandidate = VoteCandidate::query()
            ->where(['resource_id' => $resourceID])
            ->get()
            ->toArray();

        if (!empty($voteCandidate)) {
            return VoteCandidate::query()
                ->where(['resource_id' => $resourceID])
                ->update(['resource_url' => $resourceUrl]);
        } else {
            return true;
        }
    }

    public function getTotalNumberOfVotes($conditions)
    {
        return VoteCandidate::query()->where($conditions)->sum('votes_number');
    }

    public function getTopItemsByConditions($conditions)
    {
        return VoteCandidate::query()->where($conditions)->orderBy('votes_number', 'desc')->limit(5)->get();
    }

    public function getItemsByConditions($conditions, $limit_num = 0)
    {
        if ($limit_num > 0) {
            $data = VoteCandidate::query()->where($conditions)->orderBy('votes_number', 'desc')->limit($limit_num)->get();
        } else {
            $data = VoteCandidate::query()->where($conditions)->orderBy('votes_number', 'desc')->get();
        }

        return $data;
    }

    public function getItemsByGroup($groupId = 'all', $activityId, $limit = 0)
    {
        $query = VoteCandidate::query()->orderBy('weight', 'DESC');
        $query->orderBy('code', 'ASC');
        $query->where('activity_id', $activityId);
        if ($groupId != 'all') {
            $query->where('group_id', $groupId);
        }
        if ($limit > 0) {
            $query->limit($limit);
        }

        return $query->get();
    }

    public function getMaxOrderId($groupId = 'all', $activityId)
    {
        $query = VoteCandidate::query();
        $query->where('activity_id', $activityId);
        if ($groupId != 'all') {
            $query->where('group_id', $groupId);
        }

        return $query->max('order_id');
    }

    public function getMaxCode($activityId)
    {
        $query = VoteCandidate::query();
        $query->where('activity_id', $activityId);

        return $query->count();
    }

    public function getGroupIdByActivityId($activityId, $limit)
    {
        return VoteCandidate::query()->distinct('group_id')
            ->where(['activity_id', $activityId])
            ->orderBy('order_id', 'asc')
            ->limit($limit)->get();
    }

    public function getIdByActivityId($activityId)
    {
        return VoteCandidate::query()->select(['id'])
            ->where('activity_id', $activityId)
            ->get();
    }

    /**
     * @param VoteCandidate $voteCandidate
     *
     * @return VoteCandidate|null
     */
    public function create($data)
    {
        $res = VoteCandidate::query()->create($data);

        return $res;
    }

    /**
     * @param $conditions
     *
     * @return mixed
     */
    public function where($conditions)
    {
        return VoteCandidate::query()->where($conditions)->get();
    }


    public function getByActivityId($activityId)
    {
        return VoteCandidate::with('voteGroup')->where(['activity_id' => $activityId])->get();
    }

    /**
     * @param $updates
     * @param $conditions
     *
     * @return mixed
     */
    public function update($updates, $conditions)
    {
        return VoteCandidate::query()->where($conditions)->update($updates);
    }

    /**
     * @param $id
     *
     * @return mixed
     */
    public function findById($id)
    {
        return VoteCandidate::find($id);
    }

    /**
     * @param Array $id
     *
     * @return mixed
     */
    public function softDelete($ids)
    {
        return VoteCandidate::query()->whereIn('id', $ids)->delete();
    }
}
